package com.luoqiz.project.mulThreadDownload;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;

public class AppMainUI extends JFrame {

	private static final long serialVersionUID = -60322598447687761L;

	private Downloader dl;

	protected static JProgressBar[] bars;

	public static void main(String[] args) {
		new AppMainUI();
	}

	public AppMainUI() {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		this.setPreferredSize(new Dimension(550, 450));
		int frameWidth = this.getPreferredSize().width;
		int frameHeight = this.getPreferredSize().height;
		this.setSize(frameWidth, frameHeight);
		this.setLocation((screenSize.width - frameWidth) / 2, (screenSize.height - frameHeight) / 2);

		this.setLayout(null);
		this.setVisible(true);
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(-1);
			}
		});
		this.setTitle("多线程下载器");
		init();
	}

	public void init() {
		JPanel firstLine = new JPanel();
		firstLine.setBounds(0, 0, 550, 40);
		firstLine.setLayout(null);
		firstLine.setBackground(Color.green);

		JLabel jb1 = new JLabel("URL地址");
		jb1.setBounds(10, 10, 80, 30);
		firstLine.add(jb1);

		JTextField urlText = new JTextField(
				"https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24-windows-x64.zip"
		// "http://download.springsource.com/release/STS/3.9.2.RELEASE/dist/e4.7/spring-tool-suite-3.9.2.RELEASE-e4.7.2-win32-x86_64.zip"
		// "http://47.95.1.190:8080/1.txt"
		);
		urlText.setBounds(100, 10, 400, 30);
		firstLine.add(urlText);
		this.add(firstLine);

		JPanel secLine = new JPanel();
		secLine.setBounds(0, 40, 550, 40);
		secLine.setLayout(null);
		secLine.setBackground(Color.green);
		this.add(secLine);

		JLabel jb2 = new JLabel("保存地址");
		jb2.setBounds(10, 10, 80, 30);
		secLine.add(jb2);

		JTextField destText = new JTextField("D:\\dome\\apache.zip");
		destText.setBounds(100, 10, 400, 30);
		secLine.add(destText);

		JPanel threeLine = new JPanel();
		threeLine.setBounds(0, 80, 550, 40);
		threeLine.setLayout(null);
		threeLine.setBackground(Color.green);
		this.add(threeLine);

		JLabel jb3 = new JLabel("线程数");
		jb3.setBounds(10, 10, 80, 30);
		threeLine.add(jb3);

		JTextField threadText = new JTextField("3");
		threadText.setBounds(100, 10, 400, 30);
		threeLine.add(threadText);

		JPanel fourLine = new JPanel();
		fourLine.setBounds(0, 120, 550, 40);
		fourLine.setLayout(null);
		fourLine.setBackground(Color.green);
		this.add(fourLine);

		JPanel fiveLine = new JPanel();
		fiveLine.setBounds(0, 160, 550, 300);
		fiveLine.setLayout(null);
		fiveLine.setBackground(Color.green);
		this.add(fiveLine);

		JButton startBtn = new JButton("开始");
		startBtn.setBounds(20, 5, 100, 30);
		startBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String srcPath = urlText.getText();
				String destPath = destText.getText();
				int threadCount = Integer.parseInt(threadText.getText());
				dl = new Downloader(srcPath, destPath, threadCount);
				List<DownloadInfo> infos = dl.getInfos();
				this.addBarInfos(infos);
				dl.start();
			}

			private void addBarInfos(List<DownloadInfo> infos) {
				bars = new JProgressBar[infos.size()];
				for (DownloadInfo dl : infos) {
					bars[dl.getIndex()] = new JProgressBar();
					bars[dl.getIndex()].setMaximum((int) (dl.getEndPoint() - dl.getStartPoint()));
					bars[dl.getIndex()].setValue((int) dl.getAmount());
					bars[dl.getIndex()].setBounds(0, 10 + dl.getIndex() * 40, 500, 40);
					fiveLine.add(bars[dl.getIndex()]);
				}
				fiveLine.repaint();
			}
		});
		fourLine.add(startBtn);
		
		JButton pauseBtn = new JButton("暂停");
		pauseBtn.setBounds(140, 5, 100, 30);
		pauseBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dl.pause();
			}
		});
		fourLine.add(pauseBtn);
		
		JButton endBtn = new JButton("结束");
		endBtn.setBounds(240, 5, 100, 30);
		endBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				DownloaderThread.setPause(false);
				DownloaderThread.setDown(false);
			}
		});
		fourLine.add(endBtn);

		this.repaint();
	}
}
